<?php 
class category_models_index extends models{
	public function get_categories($parent,$filter=''){
    	$this->db->select('*')->
    					from('category')->
    					where(array('parent'=>$parent,'active'=>1))->
    					order_by('order');
    	if(!empty($filter)) $this->db->like('name', $filter);
    	$get= $this->db->get(); 
    	return $get->result();
    }
    public function get_link_category($id){    	
    	if($row=self::get_category_by_id($id)){	    	 
	    	return $row->link;
    	}
    	return false;
    }

    public function get_name_category($id){
    	if($row=self::get_category_by_id($id)){
    		return $row->name;
    	}
    	return false;
    }
    public function get_category_by_id($id){
    	$get= $this->db->select('*')->
    	from('category')->
    	where(array('id'=>$id))->
    	order_by('order')->
    	get();
    	if($get->num_rows()){
    		$row= $get->result();
    		return $row[0];
    	}
    	return false;
    }
    public function  get_top_categories( $limit=5){
    	$get= $this->db->select('*')->
    	from('category')->
    	order_by('count')->
    	limit($limit)->
    	get();
    	return $get->result();
    
    }
   
    public function add_categories($data){
    	if(!isset($data['link'])){
    		if($data['parent']){
    			$infoParent=self::get_category($data['parent']);
    			$data['link']=$infoParent[0]->link.'/'.$data['slug'];
    		}else $data['link']=$data['slug'];
    		
    		//rejet if it exist link 
    		$get= $this->db->select('*')->
		    		from('category')->
		    		where(array('link'=>$data['link'],'active'=>1))->
    				get();
    		if ($get->num_rows) {
    			$_SESSION['notification'][]='There is '.$data['link'].', Please change ';
    			return $this;
    		}
    	}
    	$infoParent=self::get_categories($data['parent']);
    	$data['order']=count($infoParent)+1;
    	$data['keywords']=self::getKeywords($data['slug'].' '.$data['description']);
    	$sql = $this->db->insert_string('category', $data);
    	$str=$this->db->query($sql);
    	return $str;
    
    }
    public function update_categories($data){
		if(isset($data['id'])){
			if(!isset($data['link']) and !empty($data['slug'])){
				if($data['parent']){
					$infoParent=self::get_category($data['parent']);
					$data['link']=$infoParent[0]->link.'/'.$data['slug'];
				}else $data['link']=$data['slug'];
			}
			$description=isset($data['description'])?' '.$data['description']:'';
			if(!isset($data['keywords']) or (isset($data['keywords']) and empty($data['keywords'])))
				$data['keywords']=self::getKeywords($data['slug'].$description);
			 
	    	$this->db->where('id', $data['id']);
	    	unset($data['id']);
	    	$this->db->update('category', $data);
	    	return $this;
		}
    
    }
    private function getKeywords($str){
    	$query= $this->db->select('word')->from('index_stop_words')->get();
    	$stop_words=array();
    	foreach ($query->result() as $row)
    		$stop_words[]= $row->word;
    	$words = extractCommonWords($str,$stop_words);
    	$keywords=implode(',', array_keys($words));
    	return $keywords;
    }
    public function delete_categories($id){
		if(isset($id)){
	    	$this->db->delete('category', array('id' => $id));
	    	$this->db->delete('category', array('parent' => $id)); 
	    	return $this;
		}
    
    }
}